import api from "@/api/apiServer";
//客户数据
let customerOption;
let warehouseOpton;
let UserOption;
async function getcustomer() {
  let res = await api.customerList();
  customerOption = res.data;
}
async function getWarehouse() {
  let res = await api.wareHouseList();
  warehouseOpton = res.data;
}
async function getUser() {
  let res = await api.UserAll();
  console.log(res, 88);
  UserOption = res.data;
}
getcustomer();
getWarehouse();
getUser();
//格式化客户数据
function format(value, option) {
  let result = option.filter((item) => {
    return item.id == value;
  });
  if (result.length > 0) {
    return result[0].customerName;
  } else {
    return "";
  }
}

//格式化仓库数据
function formatWarehouse(value, option) {
  let result = option.filter((item) => {
    return item.id == value;
  });
  if (result.length > 0) {
    return result[0].wareHouseName;
  } else {
    return "";
  }
}
//格式化人员数据
function formatUser(value, option) {
  if (option.length > 0) {
    let result = option.filter((item) => {
      return item.id == value;
    });
    if (result.length > 0) {
      return result[0].name;
    } else {
      return "";
    }
  }
}

let sourceData;
async function getData(parentCode) {
  let res = await api.DictFindByCode({ parentCode });
  sourceData = res.data;
}
getData("dict.source");

// 格式化数据来源
function caseSource(value, option) {
  let result = option.filter((item) => {
    return item.id === value;
  });

  if (result.length > 0) {
    return result[0].dictName;
  } else {
    return "";
  }
}

function caseBom(type) {
  switch (type) {
    case 0:
      return "否";
    case 1:
      return "是";
  }
}

const CommodityColumns = [
  {
    title: "序号",
    dataIndex: "index",
    key: "index",
    customRender: (text, record, index) => `${index + 1}`,
    align: "center",
  },
  {
    title: "商品编号",
    dataIndex: "goodsCode",
    key: "goodsCode",
    align: "center",
  },
  {
    title: "商品名称",
    dataIndex: "goodsName",
    key: "goodsName",
    align: "center",
  },
  {
    title: "商品类型",
    key: "goodsTypeName",
    dataIndex: "goodsTypeName",
    align: "center",
  },
  {
    title: "计量单位",
    key: "unitName",
    dataIndex: "unitName",
    align: "center",
  },

  {
    title: "规格",
    key: "specifications",
    dataIndex: "specifications",
    align: "center",
  },
  {
    title: "品牌",
    key: "brand",
    dataIndex: "brand",
    align: "center",
  },
  {
    title: "采购单价",
    key: "unitPrice",
    dataIndex: "unitPrice",
    align: "center",
  },
  {
    title: "重量",
    key: "weight",
    dataIndex: "weight",
    align: "center",
  },
  {
    title: "备注",
    key: "remark",
    dataIndex: "remark",
    align: "center",
  },
];
const goodColumns = [
  {
    title: "序号",
    dataIndex: "index",
    key: "index",
    customRender: (text, record, index) => `${index + 1}`,
    align: "center",
  },
  {
    title: "商品编号",
    dataIndex: "goodsCode",
    key: "goodsCode",
    align: "center",
  },
  {
    title: "商品名称",
    dataIndex: "goodsName",
    key: "goodsName",
    align: "center",
  },
  {
    title: "商品类型",
    key: "goodsTypeName",
    dataIndex: "goodsTypeName",
    align: "center",
  },
  {
    title: "计量单位",
    key: "unitName",
    dataIndex: "unitName",
    align: "center",
  },
  {
    title: "来源",
    key: "source",
    dataIndex: "source",
    align: "center",
    customRender: (text, record, index) => {
      return caseSource(text, sourceData);
    },
  },
  {
    title: "规格",
    key: "specifications",
    dataIndex: "specifications",
    align: "center",
  },
  {
    title: "品牌",
    key: "brand",
    dataIndex: "brand",
    align: "center",
  },
  {
    title: "是否生成BOM",
    key: "isUsedBom",
    dataIndex: "isUsedBom",
    customRender: (text, record, index) => {
      return caseBom(text);
    },
    align: "center",
  },
];

const columns = [
  {
    title: "序号",
    dataIndex: "index",
    key: "index",
    width: 100,
    customRender: (text, record, index) => `${index + 1}`,
    align: "center",
  },
  {
    title: "订单号",
    dataIndex: "orderNumber",
    key: "orderNumber",
    align: "center",
    width: 200,
    // fixed: 'left'
  },
  {
    title: "客户",
    dataIndex: "customer",
    key: "customer",
    align: "center",
    customRender: (text, record, index) => {
      return format(text, customerOption);
    },
  },
  {
    title: "客户单号",
    dataIndex: "customerOrderCode",
    key: "customerOrderCode",
    align: "center",
    width: 250,
  },
  {
    title: "商品",
    dataIndex: "goodsNames",
    key: "goodsNames",
    align: "center",
  },
  {
    title: "备注",
    dataIndex: "remarks",
    key: "remarks",
    align: "center",
    width: 250,
  },
  {
    title: "订单日期",
    dataIndex: "orderDate",
    key: "orderDate",
    align: "center",
    width: 200,
  },

  {
    title: "送货地址",
    dataIndex: "address",
    key: "address",
    align: "center",
  },
  {
    title: "计划送货日期",
    dataIndex: "plannedDeliveryDate",
    key: "plannedDeliveryDate",
    align: "center",
    width: 200,
  },
  {
    title: "销售员",
    dataIndex: "salesPerson",
    key: "salesPerson",
    align: "center",
    customRender: (text, record, index) => {
      return formatUser(text, UserOption);
    },
  },
  {
    title: "计划收款日期",
    dataIndex: "plannedCollectionDate",
    key: "plannedCollectionDate",
    align: "center",
    width: 200,
  },
  {
    title: "发票号",
    dataIndex: "invoiceNo",
    key: "invoiceNo",
    align: "center",
  },
  {
    title: "收款账号",
    dataIndex: "collectionAccount",
    key: "collectionAccount",
    align: "center",
  },
  {
    title: "总金额",
    dataIndex: "totalAmount",
    key: "totalAmount",
    align: "center",
  },
  {
    title: "收款金额",
    dataIndex: "amountCollected",
    key: "amountCollected",
    align: "center",
  },
  {
    title: "结算币种",
    dataIndex: "settlementCurrency",
    key: "settlementCurrency",
    align: "center",
  },

  {
    width: 150,
    title: "操作",
    dataIndex: "action",
    key: "action",
    align: "center",
    scopedSlots: { customRender: "action" },
    fixed: "right",
  },
];

const saleColumns = [
  {
    title: "序号",
    dataIndex: "index",
    key: "index",
    width: 100,
    customRender: (text, record, index) => `${index + 1}`,
    align: "center",
  },
  {
    title: "订单号",
    dataIndex: "orderNumber",
    key: "orderNumber",
    align: "center",
    width: 200,
    // fixed: 'left'
  },
  {
    title: "客户",
    dataIndex: "customer",
    key: "customer",
    align: "center",
    customRender: (text, record, index) => {
      return format(text, customerOption);
    },
  },
  {
    title: "客户单号",
    dataIndex: "customerOrderCode",
    key: "customerOrderCode",
    align: "center",
    width: 250,
  },
  {
    title: "商品",
    dataIndex: "goodsNames",
    key: "goodsNames",
    align: "center",
  },
  {
    title: "备注",
    dataIndex: "remarks",
    key: "remarks",
    align: "center",
    width: 250,
  },
  {
    title: "订单日期",
    dataIndex: "orderDate",
    key: "orderDate",
    align: "center",
    width: 200,
  },

  {
    title: "送货地址",
    dataIndex: "address",
    key: "address",
    align: "center",
  },
  {
    title: "计划送货日期",
    dataIndex: "plannedDeliveryDate",
    key: "plannedDeliveryDate",
    align: "center",
    width: 200,
  },
  {
    title: "销售员",
    dataIndex: "salesPerson",
    key: "salesPerson",
    align: "center",
    customRender: (text, record, index) => {
      return formatUser(text, UserOption);
    },
  },
  {
    title: "计划收款日期",
    dataIndex: "plannedCollectionDate",
    key: "plannedCollectionDate",
    align: "center",
    width: 200,
  },
  {
    title: "发票号",
    dataIndex: "invoiceNo",
    key: "invoiceNo",
    align: "center",
  },
  {
    title: "收款账号",
    dataIndex: "collectionAccount",
    key: "collectionAccount",
    align: "center",
  },
  {
    title: "总金额",
    dataIndex: "totalAmount",
    key: "totalAmount",
    align: "center",
  },
  {
    title: "收款金额",
    dataIndex: "amountCollected",
    key: "amountCollected",
    align: "center",
  },
  {
    title: "结算币种",
    dataIndex: "settlementCurrency",
    key: "settlementCurrency",
    align: "center",
  },

 
];
const customer = [
  {
    title: "客户名称",
    dataIndex: "customerName",
    key: "customerName",
    align: "center",
    // fixed: 'left'
  },
  {
    title: "客户编号",
    dataIndex: "customerNumber",
    key: "customerNumber",
    align: "center",
  },
  {
    title: "电话号码",
    dataIndex: "telephone",
    key: "telephone",
    align: "center",
  },
  {
    title: "联系人",
    key: "contacts",
    dataIndex: "contacts",
    align: "center",
  },
  {
    title: "公司地址",
    key: "companyAddress",
    dataIndex: "companyAddress",
    align: "center",
  },
  {
    title: "电子邮箱",
    key: "email",
    align: "center",
    dataIndex: "email",
  },
  {
    title: "公司电话",
    key: "companyPhone",
    align: "center",
    dataIndex: "companyPhone",
  },
];

const user = [
  {
    title: "序号",
    width: 80,
    dataIndex: "index",
    key: "index",
    align: "center",
    customRender: (text, record, index) => `${index + 1}`,
  },
  {
    title: "员工姓名",
    dataIndex: "name",
    align: "center",
    key: "name",
  },
  {
    title: "性别",
    align: "center",
    dataIndex: "sex",
    key: "sex",
    customRender: (text, record, index) => {
      return caseSex(text);
    },
  },
  {
    title: "状态",
    align: "center",
    key: "staus",
    dataIndex: "staus",
  },
  {
    title: "部门",
    align: "center",
    key: "department",
    dataIndex: "department",
  },
  {
    title: "职务",
    align: "center",
    key: "position",
    dataIndex: "position",
  },
  {
    title: "移动电话",
    align: "center",
    key: "phone",
    dataIndex: "phone",
  },
  {
    title: "籍贯",
    align: "center",
    key: "nativePlace",
    dataIndex: "nativePlace",
  },
];

// 客户选择参数配置项
const customerModel = {
  columns: customer,
  replaceFields: {
    children: "customerTypes",
    title: "customerTypeName",
    key: "id",
    value: "id",
  },
  treeKey: "customerTypeList",
  tableKey: "customerPageList",
  searchKey: "customerTypeCode",
  name: "customerName",
  filename: "CustomerModal",
  code: "customerNumber",
};

// 用户选择配置项
const userModel = {
  columns: user,
  userReplaceFields: {
    children: "childList",
    title: "departmentName",
    key: "id",
    value: "id",
  },
  userTreeKey: "DepartmentTree",
  userTableKey: "UserList",
  userSearchKey: "departmentId",
  userName: "name",
  userfilename: "UserModal",
};

function caseSex(value) {
  switch (value) {
    case 0:
      return "女";
    case 1:
      return "男";
  }
}

export {
  columns,
  saleColumns,
  customerModel,
  userModel,
  CommodityColumns,
  goodColumns,
};
